<template>
  <div class="admin-table">
    <div class="table-header">
      <div class="table-title">
        <h3>{{ title }}</h3>
      </div>
      <div class="table-actions">
        <button v-if="showCreate" @click="handleCreate" class="btn btn-primary">
          新增
        </button>
      </div>
    </div>
    
    <div class="table-filters">
      <input 
        v-if="showSearch" 
        type="text" 
        v-model="searchQuery" 
        placeholder="搜索..." 
        class="search-input"
        @input="handleSearch"
      />
    </div>
    
    <div class="table-container">
      <table>
        <thead>
          <tr>
            <th v-for="column in columns" :key="column.key">{{ column.title }}</th>
            <th v-if="showActions">操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="item in data" :key="item.id">
            <td v-for="column in columns" :key="column.key">
              <span v-if="column.type === 'image' && item[column.key]">
                <img :src="item[column.key]" :alt="column.title" class="table-image" />
              </span>
              <span v-else-if="column.type === 'date'">
                {{ formatDate(item[column.key]) }}
              </span>
              <span v-else-if="column.type === 'status'">
                <span :class="['status-badge', getStatusClass(item[column.key])]">
                  {{ getStatusText(item[column.key]) }}
                </span>
              </span>
              <span v-else>
                {{ item[column.key] }}
              </span>
            </td>
            <td v-if="showActions">
              <button @click="handleEdit(item)" class="btn btn-small btn-secondary">编辑</button>
              <button @click="handleDelete(item)" class="btn btn-small btn-danger">删除</button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    
    <div class="table-pagination" v-if="showPagination">
      <div class="pagination-info">
        显示第 {{ (pagination.page - 1) * pagination.size + 1 }} 到 {{ Math.min(pagination.page * pagination.size, pagination.total) }} 条记录，总共 {{ pagination.total }} 条记录
      </div>
      <div class="pagination-controls">
        <button 
          @click="changePage(pagination.page - 1)" 
          :disabled="pagination.page <= 1"
          class="btn btn-small"
        >
          上一页
        </button>
        <span class="page-info">
          第 {{ pagination.page }} 页，共 {{ Math.ceil(pagination.total / pagination.size) }} 页
        </span>
        <button 
          @click="changePage(pagination.page + 1)" 
          :disabled="pagination.page >= Math.ceil(pagination.total / pagination.size)"
          class="btn btn-small"
        >
          下一页
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, watch } from 'vue'

export default {
  name: 'AdminTable',
  props: {
    title: {
      type: String,
      required: true
    },
    columns: {
      type: Array,
      required: true
    },
    data: {
      type: Array,
      required: true
    },
    pagination: {
      type: Object,
      default: () => ({ page: 1, size: 10, total: 0 })
    },
    showActions: {
      type: Boolean,
      default: true
    },
    showCreate: {
      type: Boolean,
      default: true
    },
    showSearch: {
      type: Boolean,
      default: true
    },
    showPagination: {
      type: Boolean,
      default: true
    }
  },
  emits: ['create', 'edit', 'delete', 'search', 'change-page'],
  setup(props, { emit }) {
    const searchQuery = ref('')
    
    const handleCreate = () => {
      emit('create')
    }
    
    const handleEdit = (item) => {
      emit('edit', item)
    }
    
    const handleDelete = (item) => {
      if (confirm('确定要删除这条记录吗？')) {
        emit('delete', item)
      }
    }
    
    const handleSearch = () => {
      emit('search', searchQuery.value)
    }
    
    const changePage = (page) => {
      emit('change-page', page)
    }
    
    const formatDate = (dateString) => {
      if (!dateString) return ''
      const date = new Date(dateString)
      return date.toLocaleDateString('zh-CN') + ' ' + date.toLocaleTimeString('zh-CN')
    }
    
    const getStatusClass = (status) => {
      const statusMap = {
        0: 'status-inactive',
        1: 'status-active',
        2: 'status-pending'
      }
      return statusMap[status] || 'status-default'
    }
    
    const getStatusText = (status) => {
      const statusTextMap = {
        0: '禁用',
        1: '启用',
        2: '待审核'
      }
      return statusTextMap[status] || '未知'
    }
    
    return {
      searchQuery,
      handleCreate,
      handleEdit,
      handleDelete,
      handleSearch,
      changePage,
      formatDate,
      getStatusClass,
      getStatusText
    }
  }
}
</script>

<style scoped>
.admin-table {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  overflow: hidden;
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
}

.table-title h3 {
  margin: 0;
  color: #2c3e50;
}

.btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s;
}

.btn-primary {
  background-color: #3498db;
  color: white;
}

.btn-primary:hover {
  background-color: #2980b9;
}

.btn-secondary {
  background-color: #95a5a6;
  color: white;
}

.btn-secondary:hover {
  background-color: #7f8c8d;
}

.btn-danger {
  background-color: #e74c3c;
  color: white;
}

.btn-danger:hover {
  background-color: #c0392b;
}

.btn-small {
  padding: 4px 8px;
  font-size: 12px;
}

.table-filters {
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
}

.search-input {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  width: 300px;
  font-size: 14px;
}

.table-container {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #eee;
}

th {
  background-color: #f8f9fa;
  font-weight: 600;
  color: #2c3e50;
}

.table-image {
  width: 40px;
  height: 40px;
  object-fit: cover;
  border-radius: 4px;
}

.status-badge {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.status-active {
  background-color: #d4edda;
  color: #155724;
}

.status-inactive {
  background-color: #f8d7da;
  color: #721c24;
}

.status-pending {
  background-color: #fff3cd;
  color: #856404;
}

.status-default {
  background-color: #e2e3e5;
  color: #383d41;
}

.table-pagination {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-top: 1px solid #eee;
}

.pagination-info {
  color: #7f8c8d;
  font-size: 14px;
}

.pagination-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.page-info {
  color: #7f8c8d;
  font-size: 14px;
  margin: 0 10px;
}
</style>